import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

import '../config/const.dart';

class CommonDialog extends StatefulWidget {
  final String? title;
  final String? content;
  final String? imgText;
  final String? btnText;
  final Function? callBack;
  final bool isHead;
  final bool autoDismiss;
  const CommonDialog({super.key, this.title, this.content, this.imgText, this.btnText, this.callBack, this.isHead = false, this.autoDismiss = false});

  @override
  State<CommonDialog> createState() => _CommonDialogState();
}

class _CommonDialogState extends State<CommonDialog> {

  Timer? _timer;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if(widget.autoDismiss){
      _timer = Timer(Duration(seconds: 3), (){
        SmartDialog.dismiss();
      });
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _timer?.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      // width: 600.w,
      constraints: BoxConstraints(
        maxWidth: 1.sw - 80.w
      ),
      decoration: BoxDecoration(
        color: Color(0xff1E1D20),
        borderRadius: BorderRadius.circular(20.w)
      ),
      padding: EdgeInsets.symmetric(horizontal: 32.w,vertical: 32.w),
      child:Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: [
          Visibility(visible: widget.isHead,child: Container(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                GestureDetector(
                  onTap: (){
                    SmartDialog.dismiss();
                  },
                  child: Image.asset('assets/common/common_close.png',width: 56.w,),
                )
              ],
            ),
          ),),
          Visibility(visible: widget.imgText != null,child: Container(
            margin: EdgeInsets.only(bottom: 20.w),
            child: Image.asset(widget.imgText??'',width: 96.w,height: 96.w,),
          ),),
          Visibility(visible: widget.title!=null,child: Container(
            margin: EdgeInsets.only(bottom: widget.content != null ? 10.w:widget.btnText != null ? 30.w:0),
            child: Text(widget.title??'',style: TextStyle(
              color: inputColor,
              fontWeight: FontWeight.w600,
              fontSize: 36.sp,
            ),),
          ),),
          Visibility(visible: widget.content!=null, child: Container(
            margin: EdgeInsets.only(bottom: widget.btnText != null ? 30.w:0),
            child: Text(widget.content??'',style: TextStyle(
              color: inputColor,
              fontWeight: FontWeight.w400,
              fontSize: 28.sp,
            ),textAlign: TextAlign.center,),
          )),
          Visibility(visible: widget.btnText!=null,
              child: GestureDetector(
                onTap: (){
                  SmartDialog.dismiss();
                  if(widget.callBack!=null){
                    widget.callBack!();
                  }
                },
                child: Container(
                  width: 520.w,
                  height: 108.w,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      color: themeColor,
                      borderRadius: BorderRadius.circular(8.w)
                  ),
                  child: Text(widget.btnText??'',style: TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.w600,
                    fontSize: 28.sp,
                  ),),
                ),
              )
          )
        ],
      ),
    );
  }

}

openCommonDialog({
 String? title,
 String? content,
 String? imgText,
 String? btnText,
 Function? callBack,
 bool? isHead,
  bool? autoDismiss
}){
  SmartDialog.show(
      alignment: Alignment.center,
      builder: (BuildContext context){
        return CommonDialog(
          title: title,
          content: content,
          callBack: callBack,
          imgText: imgText,
          btnText: btnText,
          isHead: isHead ?? false,
          autoDismiss: autoDismiss ?? false,
        );
      }
  );
}

